<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Config;

/**
 * 首页接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Headers:token');
        parent::_initialize();
    }

    /**
     * 首页
     * 
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 首页轮播图
     */
    public function rotation()
    {
        $result = Db('rotation')->select();
        foreach ($result as $key => $value) {
            $result[$key]['logo_image'] = Config('upload.cdnurl').$value['logo_image'];
        }
        $this->error('成功',$result);
    }

    /**
     * 案例数据
     */
    public function caseList()
    {
        $data = [];
        $appwap = Db('appwap')->where(['status' => 1])->order('sort_number desc')->select();
        foreach ($appwap as $key => $value) {
            $appwap[$key]['cover_image'] = Config('upload.cdnurl').$value['cover_image'];
        }
        /*案例appwap数据*/
        $data['appwap'] = $appwap;
        $storewap = Db('storewap')->where(['status' => 1])->order('sort_number desc')->select();
        foreach ($storewap as $key => $value) {
            $storewap[$key]['cover_image'] = Config('upload.cdnurl').$value['cover_image'];
        }
        /*案例商家wap*/
        $data['storewap'] = $storewap;
        $small_program = Db('small_program')->where(['status' => 1])->order('sort_number desc')->select();
        foreach ($small_program as $key => $value) {
            $small_program[$key]['cover_image'] = Config('upload.cdnurl').$value['cover_image'];
        }
        $data['small_program'] = $small_program;
        $this->success('成功',$data);
    }

    /**
     * 产品数据
     */
    public function product()
    {
        $data = [];
        $productapp = Db('productapp')->where(['status' => 1])->order('sort_num desc')->select();
        foreach ($productapp as $key => $value) {
            $productapp[$key]['cover_image'] = Config('upload.cdnurl').$value['cover_image'];
        }
        /*产品app数据*/
        $data['productapp'] = $productapp;
        $productsmall = Db('productsmall')->where(['status' => 1])->order('sort_num desc')->select();
        foreach ($productsmall as $key => $value) {
            $productsmall[$key]['cover_image'] = Config('upload.cdnurl').$value['cover_image'];
        }
        /*产品小程序*/
        $data['productsmall'] = $productsmall;

        $productstore = Db('productstore')->where(['status' => 1])->order('sort_num desc')->select();
        foreach ($productstore as $key => $value) {
            $productstore[$key]['cover_image'] = Config('upload.cdnurl').$value['cover_image'];
        }
        $data['productstore'] = $productstore;
        $this->success('成功',$data);
    }

    /**
     * 产品轮播图
     */
    public function productCover()
    {
        $result = Db('productcover')->select();
        foreach ($result as $key => $value) {
            $result[$key]['cover_image'] = Config('upload.cdnurl').$value['cover_image'];
        }
        $this->success('成功',$result);
    }

    /**
     * 公司简介
     */
    public function companyProfile()
    {
        $data = [];
        /*公司简介*/
        $result = Db('profile')->where(['status' =>1])->find();
        $data['profile'] = $result;
        /*联系方式*/
        $lianxi = Db('website')->where(['id' => 1])->find();
        $lianxi['websitelogo_image'] = Config('upload.cdnurl').$lianxi['websitelogo_image'];
        $lianxi['wechatLogo_image'] = Config('upload.cdnurl').$lianxi['wechatLogo_image'];
        $lianxi['QQLogo_image'] = Config('upload.cdnurl').$lianxi['QQLogo_image'];
        $lianxi['music_file'] = Config('upload.cdnurl').$lianxi['music_file'];
        $data['contact'] = $lianxi;
        $this->success('成功',$data);
    }

    /**
     * 案例、产品模块详情
     *
     */
    public function detail()
    {
        if (empty($_REQUEST['type'])) $this->error('缺少参数');
        if (empty($_REQUEST['id'])) $this->error('缺少参数');
        $id = intval($_REQUEST['id']);
        $data = [];
        switch ($_REQUEST['type']){
            /**
             * 案例APP、WAp数据详情
             */
            case 1:
                $productapp = Db('appwap')->where(['status' => 1,'id' => $id])->find();
                $productapp['cover_image'] = Config('upload.cdnurl').$productapp['cover_image'];
                $data['appwap'] = $productapp;
                break;
            /**
             * 案例商家WAP数据详情
             */
            case 2:
                $storewap = Db('storewap')->where(['status' => 1,'id' => $id])->find();
                $storewap['cover_image'] = Config('upload.cdnurl').$storewap['cover_image'];
                $data['storewap'] = $storewap;
                break;
            /**
             * 案例小程序数据详情
             */
            case 3:
                $small_program = Db('small_program')->where(['status' => 1,'id' => $id])->find();
                $small_program['cover_image'] = Config('upload.cdnurl').$small_program['cover_image'];
                $data['small_program'] = $small_program;
                break;
            /**
             * 产品app、wap详情
             */
            case 4:
                $productapp = Db('productapp')->where(['status' => 1,'id'=>$_REQUEST['id']])->find();
                $productapp['cover_image'] = Config('upload.cdnurl').$productapp['cover_image'];
                $data['productapp'] = $productapp;
                break;
            /**
             * 产品a小程序详情
             */
            case 5:
                $productsmall = Db('productsmall')->where(['status' => 1,'id'=>$_REQUEST['id']])->find();
                $productsmall['cover_image'] = Config('upload.cdnurl').$productsmall['cover_image'];
                $data['productsmall'] = $productsmall;
                break;
            /**
             * 产品商家详情
             */
            case 6:
                $productstore = Db('productstore')->where(['status' => 1,'id'=>$_REQUEST['id']])->find();
                $productstore['cover_image'] = Config('upload.cdnurl').$productstore['cover_image'];
                $data['productstore'] = $productstore;
                break;

        }
        $this->success('成功',$data);
    }


}
